const RebateGoods = require('../models/RebateGoods');

//得到不同类目的商品
exports.findAll = async (ctx)=>{
    const category = ctx.query.category;
    let number = ctx.query.number;
    let size = ctx.query.size;
    number = number ==null?0:number < 0 ? 0:number;
    size = size ==null?5:size < 0 ? 5:size;
    number = parseInt(number);
    size = parseInt(size);
    let offset = number*size;
    console.log(category);
    try{
        let goods;
        //查全部
        if(category == null){
            goods = await RebateGoods.findAndCountAll({//https://itbilu.com/nodejs/npm/V1PExztfb.html#api-findAndCount
                attributes:['id','name','mainImg','category','shopName',
                    'price','sales',
                    'couponCapacity','couponRest',
                    'startTime','endTime',
                    'couponLink','couponAmount',
                ],
                where:{
                    isEnd:"否"
                },
                limit: size,
                offset: offset,
                order: [
                    ['startTime', 'DESC'],
                    ['price', 'ASC'],
                    ['createdTime', 'DESC'],
                    ['endTime', 'DESC'],
                ],
            });
        }else{
            goods = await RebateGoods.findAndCountAll({
                attributes:['id','name','mainImg','category','shopName',
                    'price','sales',
                    'couponCapacity','couponRest',
                    'startTime','endTime',
                    'couponLink','couponAmount',
                ],
                where:{
                    type:type,
                    isEnd:"否"
                },
                limit: size,
                offset: offset,
                order: [
                    ['startTime', 'DESC'],
                    ['price', 'ASC'],
                    ['createdTime', 'DESC'],
                    ['endTime', 'DESC'],
                ],
            });
        }
        if(goods.length===0){
            ctx.body = {
                code:0,
                number:number,
                size:size,
                totalElements:0,
                totalPages:0,
                data:[]
            };
            return;
        }
        let totalElements = goods.count;
        let totalPages = Math.ceil(totalElements/size) ;

        ctx.body = {
            code: 0,
            number: number,
            size: size,
            totalElements: totalElements,
            totalPages:totalPages,
            list: goods.rows
        }
    }
    catch(e){
        ctx.body = {
            code:10000,
            message:'网络错误'
        }
    }
}


//得到商品详情页信息
exports.findOne = async (ctx)=>{
    const id = ctx.query.id;
    try{
        const goods = await RebateGoods.findOne({
            attributes:['id','name','mainImg','category','shopName',
                'price','sales',
                'couponCapacity','couponRest',
                'startTime','endTime',
                'couponLink','couponAmount',
            ],
            where:{
                id:id
            },
        });
        ctx.body = {
            code:0,
            data:goods
        }
    }
    catch(e){
        ctx.body = {
            code:10000,
            message:'网络错误'
        }
    }
}